package edu.jd.block_chain.forum.freeChat.impl;

import edu.jd.block_chain.forum.freeChat.FreeChatDto;
import edu.jd.block_chain.forum.freeChat.FreeChatService;
import edu.jd.block_chain.forum.freeChat.HostChat;
import edu.jd.block_chain.forum.freeChat.dao.FreeChatDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @descriptions:
 * @data: 2021/11/24 19:54
 */
@Service
@Transactional
public class FreeChatServiceImpl implements FreeChatService {
    @Resource
    private FreeChatDao freeChatDao;

    @Override
    public List<HostChat> getHostChat(Integer p_id) {
        List<HostChat> hostChats = freeChatDao.getHostChatList(p_id);
        for (HostChat hostChat : hostChats) {
            hostChat.setChildren(freeChatDao.getSubChatListByHostId(hostChat.getF_c_host_id()));
        }
        return hostChats;
    }

    @Override
    public void addHostChat(FreeChatDto dto) {
        Integer hostId = freeChatDao.getHostId(dto.getP_id());
        dto.setF_c_host_id(hostId==null?0:hostId);
        freeChatDao.addHostChat(dto);
    }

    @Override
    public void addSubChat(FreeChatDto dto) {
        Integer subId = freeChatDao.getSubId(dto.getP_id(),dto.getF_c_host_id());
        System.out.println(subId);
        dto.setF_c_sub_id(subId==null?0:subId);
        freeChatDao.addSubChat(dto);
    }
}
